package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.VNSmartUI;
import vns.client.event.ObjectEditEvent;
import vns.client.helper.RPCCall;
import vns.client.service.ObjectPropertyService;
import vns.client.service.ObjectPropertyServiceAsync;
import vns.client.service.ObjectPropertyTypeService;
import vns.client.service.ObjectPropertyTypeServiceAsync;
import vns.client.service.ObjectServiceAsync;
import vns.client.view.ObjectUI;
import vns.shared.ObjectDTO;
import vns.shared.ObjectPropertyDTO;
import vns.shared.ObjectPropertyTypeDTO;
import vns.shared.ObjectTypeDTO;


import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ObjectPresenter implements Presenter{

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		doInitTable();
	}

	public interface Display{
		Button getAddButton();
		Button getEditButton();
		Button getUpdateButton();

		ObjectDTO getCurrentObjectDTO();

		void setCurrentObjectTypeDTO(ObjectTypeDTO currentObjectTypeDTO);
		ObjectTypeDTO getCurrentObjectTypeDTO();
		
		ArrayList<ObjectDTO> getListRow();
		boolean setListRow(List<ObjectDTO> listRow);
		
		List<ObjectPropertyDTO> getListProperty();
		void setListProperty(List<ObjectPropertyDTO> listPType);

		Widget asWidget();
		void render();
		
		HTMLPanel getHTMLPanelDetail();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final ObjectServiceAsync rpcService;
	private final ObjectPropertyServiceAsync rpcPropertyService = GWT.create(ObjectPropertyService.class);
	private final ObjectPropertyTypeServiceAsync rpcOpt = GWT.create(ObjectPropertyTypeService.class);

	
	/*
	 * Public method
	 */
	
	public ObjectPresenter(ObjectUI view, SimpleEventBus eventBus, ObjectServiceAsync model, ObjectTypeDTO o){
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService= model;
		display.setCurrentObjectTypeDTO(o);
		bind();
	}
	
	/*
	 * Private method
	 */
	
	private boolean bind(){
		display.getAddButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
//				Window.alert("Goi UI tao doi tuong oTYpe " + display.getCurrentObjectTypeDTO().getName() 
//						+ "list property size " + display.getListProperty().size());
				eventBus.fireEvent(new ObjectEditEvent(
						null, 
						display.getCurrentObjectTypeDTO(),
						display.getListProperty(), 
						"ADD"));
			}
		});
		display.getUpdateButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doUpdate();
			}
		});
		display.getEditButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (display.getCurrentObjectDTO() != null) {
					eventBus.fireEvent(new ObjectEditEvent(display
							.getCurrentObjectDTO(), display
							.getCurrentObjectTypeDTO(), display
							.getListProperty(), "EDIT"));
				} else {
					Window.alert("Chon hang can chinh sua");
				}
			}
		});
		return false;
	}
	
	private void doInitTable() {
		new RPCCall<ArrayList<ObjectDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Không load được kiểu danh sách!"
						+ caught.toString());
			}

			@Override
			public void onSuccess(ArrayList<ObjectDTO> result) {
				display.getListRow().addAll(result);
				new RPCCall<ArrayList<ObjectPropertyDTO>>() {
					// load cac cot, cac thuoc tinh
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Không load được"
								+ caught.toString());
					}

					@Override
					public void onSuccess(ArrayList<ObjectPropertyDTO> result) {
						display.setListProperty(result);
						display.render();
					}

					@Override
					protected void callService(
							AsyncCallback<ArrayList<ObjectPropertyDTO>> cb2) {
						rpcPropertyService.Loadall(display.getCurrentObjectTypeDTO(), cb2);
					}
				}.retry(3);
			}

			@Override
			protected void callService(
					AsyncCallback<ArrayList<ObjectDTO>> cb) {
				rpcService.Loadall(display.getCurrentObjectTypeDTO(), cb);
			}
		}.retry(3);

	}

	private void doUpdate(){
//		Window.alert("Do Update, các phần từ được update là");
//		ArrayList<ObjectDTO> listNew = display.getListRow();
//		for(ObjectDTO o : listNew){
//			Window.alert(o.getName() + " " + Boolean.toString(o.getIsuser()) + " " + o.getDescribe());
//		}
		new RPCCall<Void>() {
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi khi đang cập nhật! \n " + caught.toString());
			}
			
			@Override
			public void onSuccess(Void result) {
				Window.alert("Cập nhật thành công! \n ");
			}
			
			@Override
			protected void callService(AsyncCallback<Void> cb) {
//				Window.alert("RPC Serive Do Update");
				//TODO
				rpcService.Updateall(display.getListRow(),display.getCurrentObjectTypeDTO(), cb);
			}
		}.retry(3);
	}
}
